import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios'
import { AlertModule } from 'vux'

import Login from '../components/LoginVux'
import Home from '../components/HomeVux'
import IndexHome from '../components/homes/Index'

import IndexMonitor from '../components/homes/monitor/Index'
import Pl3Monitor from '../components/homes/monitor/Pl3'
import Pl3DetailMonitor from '../components/homes/monitor/Pl3_Detail'

import IndexReport from '../components/homes/report/Index'
import DltReport from '../components/homes/report/Dlt'

Vue.use(Router)

const router = new Router({
  mode: 'hash',
  base: __dirname,
  /** 路由映射map **/
  routes: [
    {
      path: '/Home',
      component: Home,
      children: [
        {
          path: 'Index',
          component: IndexHome
        },
        {
          path: 'Monitor/Index',
          component: IndexMonitor
        },
        {
          path: 'Report/Index',
          component: IndexReport
        },
        {
          path: 'Report/Dlt',
          component: DltReport
        },
        {
          path: 'Monitor/Pl3',
          component: Pl3Monitor
        },
        {
          path: 'Monitor/Pl3_Detail',
          component: Pl3DetailMonitor
        }
      ]
    },
    {
      path: '/',
      component: Login
    },
    {
      path: '*',
      name: 'page404',
      redirect: '/Home/Index'
    }
  ]
})

/** 跳转前的逻辑 **/
router.beforeEach((to, from, next) => {
  console.log('before---------------' + to.path)
  if (to.path === '/') {
    next()
  } else {
    var sessionId = sessionStorage.getItem('sessionId')
    axios.get('/checkSessionId', {params: {'sessionId': sessionId}}).then(res => {
      console.log(res.data)
      var data = res.data
      if (!data.result) {
        AlertModule.show({
          title: '提示',
          content: ('用户登录已过期！')
        })
        next({
          path: '/',
          query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
      } else {
        next()
      }
    }).catch(err => {
      AlertModule.show({
        title: '提示',
        content: '用户校验异常！'
      })
      console.log(err)
      // window.location.href = '/'
      next({
        path: '/',
        query: {redirect: to.fullPath}
      })
    })
  }
})

// window.routeList = [];

// // 跳转后的逻辑
// router.afterEach(function(transition) {
//     console.log('-----------------after');
//     for (var i = 0; i < routeList.length; i++) {
//         console.log(routeList[i].name);
//     };
// });

export default router
